<?php


namespace app\api\controller;

use app\admin\validate\IDMustBePositiveInt;
use app\api\validate\CartValidate;
use app\service\model\CartModel;
use app\service\Y;

class Cart extends AuthApi
{

    /**
     * 添加购物车
     * @param CartValidate $cart_validate
     * @return \think\response\Json
     * @throws \app\service\exception\ParameterException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add_cart(CartValidate $cart_validate): \think\response\Json
    {
        $cart_validate->goCheck();
        $param = $cart_validate->getDataByRule();
        $param['member_id'] = $this->member->id;

        return CartModel::addCart($param);
    }

    /**
     * 购物车列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function cart_list()
    {
        $data = CartModel::with(['goods.images', 'sku'])->where('member_id', '=', $this->member->id)->select();
        return Y::success($data, '购物车列表');
    }

    /**
     * 修改购物车数量
     * @param IDMustBePositiveInt $IDMustBePositiveInt
     * @return \think\response\Json
     * @throws \app\service\exception\ParameterException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit_num(IDMustBePositiveInt $IDMustBePositiveInt): \think\response\Json
    {
        $IDMustBePositiveInt->goCheck();
        $id = input('param.id');
        $num = input('param.num', 0);
        if ($num <= 0) {
            return Y::error('购物车数量必须是正整数');
        }
        $where = [
            ['id', '=', $id],
            ['member_id', '=', $this->member->id]
        ];
        $cart = CartModel::where($where)->find();
        if (!$cart) {
            return Y::error('参数错误');
        }
        $cart->save(['num' => $num]);
        return Y::success([], '修改成功');
    }

    /**
     * 删除购物车
     * @param IDMustBePositiveInt $IDMustBePositiveInt
     * @return \think\response\Json
     * @throws \app\service\exception\ParameterException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function del_cart(IDMustBePositiveInt $IDMustBePositiveInt): \think\response\Json
    {
        $IDMustBePositiveInt->goCheck();
        $id = input('param.id');
        $where = [
            ['id', '=', $id],
            ['member_id', '=', $this->member->id]
        ];
        $cart = CartModel::where($where)->find();
        if (!$cart) {
            return Y::error('参数错误');
        }
        $cart->delete();
        return Y::success([], '删除成功');
    }

    /**
     * 清空购物车
     * @return \think\response\Json
     */
    public function empty_cart(): \think\response\Json
    {
        CartModel::where('member_id', '=', $this->member->id)->delete();
        return Y::success([], '操作成功');
    }
}